import os
import time
import serial
import serial.tools.list_ports
from ctypes import *

width = 2560
height = 1440
rgbNum = 8

class ColorPicker:
    ser = serial.Serial()   #实例化空的串口对象
    serStatus = 0           #串口状态
    serCom = ""             #串口号
    passFlag = 0            #正常初始化标志
    rgbData = {}

    def __init__(self, com):
        self.serCom = self.is_serial_port_exist_and_get(com)
        if len(self.serCom) == 0:
            return
        
        self.serStatus = self.open_serial(self.serCom, 115200, 8, 1, "N")
        if self.serStatus == 0:
            return
        
        self.passFlag = 1

    #寻找并获取串口号
    def is_serial_port_exist_and_get(self, com):
        portList = serial.tools.list_ports.comports()
        for port in portList:
            if str(com) in str(port):
                print(f">Find keyword [{com}]")
                print(f">Port:[{port[0]}]")
                print(f">Name:[{port[1]}]")
                return port[0]
        print(f">Not find keyword [{com}]")
        return []
    
    #打开串口
    def open_serial(self, com, baudRate, dataBits, stopBits, parityBits):
        if len(com) == 0:
            print(f">Open [{com}] fail")
            return 0
        
        self.ser.port = com
        self.ser.baudrate = baudRate
        self.ser.bytesize = dataBits
        self.ser.stopbits = stopBits
        self.ser.parity = parityBits
        self.ser.rtscts = False
        self.ser.timeout = 0.05
        self.ser.open()

        if self.ser.isOpen():
            print(f">Open [{com}] success")
            print(f">Baud rate:[{self.ser.baudrate}]")
            print(f">Data bits:[{self.ser.bytesize}]")
            print(f">Stop bits:[{self.ser.stopbits}]")
            print(f">Parity bits:[{self.ser.parity}]")
            return 1
        else:
            print(f">Open [{com}] fail")
            return 0
        
    #关闭串口
    def close_serial(self):
        if self.ser.isOpen():
            self.ser.close()
            print(f">Close [{self.ser.port}] success")
        else:
            print(f">Close [{self.ser.port}] error")

    #获取屏幕上某个像素点的颜色
    def get_screen_rgb(self, x, y):
        if(x >= width or y >= height):
            return 0
        
        color = windll.gdi32.GetPixel(windll.user32.GetDC(None), x, y)

        r = color & 0xFF
        g = (color & 0xFF00) >> 8
        b = (color & 0xFF0000) >> 16

        return r, g, b

colorPicker = ColorPicker("CH340")

if colorPicker.passFlag:
    while True:
        colorPicker.rgbData[0], colorPicker.rgbData[1], colorPicker.rgbData[2] = colorPicker.get_screen_rgb(10, int(height / 4 * 0))
        colorPicker.rgbData[3], colorPicker.rgbData[4], colorPicker.rgbData[5] = colorPicker.get_screen_rgb(10, int(height / 4 * 1))
        colorPicker.rgbData[6], colorPicker.rgbData[7], colorPicker.rgbData[8] = colorPicker.get_screen_rgb(10, int(height / 4 * 2))
        colorPicker.rgbData[9], colorPicker.rgbData[10], colorPicker.rgbData[11] = colorPicker.get_screen_rgb(10, int(height / 4 * 3))
        colorPicker.rgbData[12], colorPicker.rgbData[13], colorPicker.rgbData[14] = colorPicker.get_screen_rgb(int(width-11), int(height / 4 * 3))
        colorPicker.rgbData[15], colorPicker.rgbData[16], colorPicker.rgbData[17] = colorPicker.get_screen_rgb(int(width-11), int(height / 4 * 2))
        colorPicker.rgbData[18], colorPicker.rgbData[19], colorPicker.rgbData[20] = colorPicker.get_screen_rgb(int(width-11), int(height / 4 * 1))
        colorPicker.rgbData[21], colorPicker.rgbData[22], colorPicker.rgbData[23] = colorPicker.get_screen_rgb(int(width-11), int(height / 4 * 0))

        for i in range(len(colorPicker.rgbData)):
            print("0x%x " % colorPicker.rgbData[i])
        
        head = [0x20, len(colorPicker.rgbData)]
        data = head + list(colorPicker.rgbData.values())

        colorPicker.ser.write(data)

        time.sleep(1)